import Vue from 'vue';
import Vuex from 'vuex';
import createPersistedState from 'vuex-persistedstate';
Vue.use(Vuex);
const debug = process.env.NODE_ENV !== 'production';
// 加个版本号 迭代
import { version } from './utils/config';
export default new Vuex.Store({
  plugins: [
    // 存储
    createPersistedState({
      storage: {
        getItem: key => localStorage.getItem(`${version}_${key}`),
        setItem: (key, value) =>
          localStorage.setItem(`${version}_${key}`, value),
        removeItem: key => localStorage.removeItem(`${version}_${key}`)
      }
    })
  ],
  state: {
    access_token: '',
    // 广告账户组
    options: [],
    // facebook user_id
    user_id: '',
    loading: false
  },
  mutations: {
    setToken(state, token) {
      state.access_token = token;
    },
    setUserId(state, user_id) {
      state.user_id = user_id;
    },
    setOptions(state, options) {
      state.options = options;
    },
    setLoading(state, flag) {
      state.loading = flag;
    }
  },
  getters: {
    loading(state) {
      return state.loading;
    },
    options(state) {
      return state.options;
    }
  },
  actions: {
    clearData({ commit }) {
      commit('setToken', '');
      commit('setUserId', '');
    }
  }
});
